package servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import service.UserService;
import service.UserServiceImpl;
import user.entity.User;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name="updateServlet",urlPatterns = "/updateServlet")
public class updateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out= response.getWriter();
        UserService userService=new UserServiceImpl();
        String num=request.getParameter("uNum");
        String name=request.getParameter("uName");
        String sex=request.getParameter("uSex");
        String grade=request.getParameter("uGrade");
        String major=request.getParameter("uMajor");
        String number=request.getParameter("uNumber");
        String direction=request.getParameter("uDirection");
        String skill=request.getParameter("uSkill");
        String self=request.getParameter("uSelf");
        String password=request.getParameter("uPwd");
        User user=new User(num,name,sex,grade,major,number,direction,skill,self,password);
        boolean result=userService.updateUser(num,user);
        if(result) {
            out.print("<script>alert('修改成功');window.location.href='queryAllServlet'</script>" );
        } else
            out.print("<script>alert('修改失败');window.location.href='userInfo.jsp'</script>" );
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
